﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Logic.BusinessLogic;
using System.Drawing;
using System.Xml;
using System.Xml.Linq;

public partial class Pages_Dashboard_Account_PostStatus : System.Web.UI.Page, ICrumbedPage
{
	public int AccountID
	{
		get
		{
			int temp;
			int.TryParse(Request["ID"], out temp);
			return temp;
		}
	}
	protected BO.User User;
	protected BO.Account Account;
	private void Authenicate()
	{
		if (Authentication.IsCurrentUserAuthenticated())
			User = Authentication.GetCurrentUser();

		if (User == null)
			Response.Redirect("/default.aspx");

		Account = BO.Account.GetWithMaster(AccountID);

		if(Account == null || Account.MasterAccount.ProfileID != User.ProfileID)
			Response.Redirect("/default.aspx");		
	}

    protected void Page_Load(object sender, EventArgs e)
    {
		Authenicate();
		LoadData();
    }

	private void LoadData()
	{	
		postStatus.SingleAccount = Account;
		postStatus.User = User;

        ucAccountSideMenu.AccountID = AccountID;
		if (!IsPostBack)
		{
			var posts = Logic.BusinessLogic.Stats.GetPostsByAccountID(Account.ID, BO.Post.PostTypes.StatusLine, 5);
			repPosts.DataSource = posts;
			repPosts.DataBind();
		}
	}

	public XmlDocument PostTimeLine(long postID)
	{
		var itemDailyStats = Logic.BusinessLogic.Stats.GetPostDailyStats(postID);
		if (itemDailyStats.Count == 0)
		{
			var chartEmpty = new XElement("chart", new XElement("series"), new XElement("graphs", new XElement("graph")));
			var xdocEmpty = new XmlDocument();
			xdocEmpty.LoadXml(chartEmpty.ToString());
			return xdocEmpty;
		}

		var series = itemDailyStats.Select(item => new XElement("value", new XAttribute("xid", itemDailyStats.IndexOf(item)), item.Date.ToString("MM/dd")));


		#region for later
		//var messageType = itemDailyStats[0].PostedItem.PostedItemType;
		//switch (messageType)
		//{
		//    case BO.PostedItemsTypes.Status:
		//        break;
		//    case BO.PostedItemsTypes.Video:
		//    //get commentNum from fb,us,twitter.
		//    //get views
		//    case BO.PostedItemsTypes.BlogPost:
		//    //get commentNum from fb,us,twitter.
		//    //get views
		//    case BO.PostedItemsTypes.Event:
		//    //get commentNum from fb,us,twitter.
		//    //get views
		//    case BO.PostedItemsTypes.Photos:
		//    //get commentNum from fb,us,twitter.
		//    //get views
		//    default:
		//        break;
		//}
		#endregion

		var graphFbComments = itemDailyStats.Select(item => new XElement("value", new XAttribute("xid", itemDailyStats.IndexOf(item)), item.FbCommentNum));
		var graphTwReplys = itemDailyStats.Select(item => new XElement("value", new XAttribute("xid", itemDailyStats.IndexOf(item)), item.TwReplyNum));
		var graphRetweets = itemDailyStats.Select(item => new XElement("value", new XAttribute("xid", itemDailyStats.IndexOf(item)), item.TwRetweetNum));

		var graphs = new XElement("graphs"
			, new XElement("graph", graphFbComments, new XAttribute("gid", 0))
			, new XElement("graph", graphTwReplys, new XAttribute("gid", 1))
			, new XElement("graph", graphRetweets, new XAttribute("gid", 2))
			);
		var chart = new XElement("chart", new XElement("series", series), graphs);
		var xdoc = new XmlDocument();
		xdoc.LoadXml(chart.ToString());
		return xdoc;
	}





    #region ICrumbedPage Members

    public int Level
    {
        get { return 4; }
    }

    public string TailName { get { return "Post a status line"; } }

    #endregion
}